/*
 * Copyright (c) 2021, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,C
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "fft.h"
#include "ti_msp_dl_config.h"
#include <math.h>
#include <ti/driverlib/m0p/dl_interrupt.h>
#define FFT_LENGTH 1024
#define SAMPLE_FREQ 1024
float PI = 3.14159265;
float fft_out[FFT_LENGTH];
// #pragma DATA_SECTION(cfft, ".TI.ramfunc");
struct compx cfft[FFT_LENGTH] = {0};

int main(void) {
  SYSCFG_DL_init();

  for (int i = 0; i < FFT_LENGTH; i++) // 生成信号序列
  {
    cfft[i].real = 15 + 10 * sinf(2 * PI * i * 100 / SAMPLE_FREQ) +\
                   5.5 * sinf(2 * PI * i * 150 / SAMPLE_FREQ); // 生成实部
    cfft[i].imag = 0;
  }
  FFT(cfft, FFT_LENGTH);
  for (int k = 0; k < FFT_LENGTH; k++) {
    fft_out[k] =
        sqrtf(cfft[k].real * cfft[k].real + cfft[k].imag * cfft[k].imag);
  }

  while (1) {
    delay_cycles(3200000);
    __WFI();
  }
}
